package com.example.shiwu.service;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.example.shiwu.db.dao.UserDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.User;
import com.example.shiwu.manager.DatabaseHelper;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class UserService {

    private DatabaseHelper dbHelper;
    private UserDao<User> userDao;
    private ArrayList<User> listUser = new ArrayList<>();

//    public UserService(Context context) {
//        dbHelper = new DatabaseHelper(context);
//    }

//    public boolean login(String name,String password){
//        SQLiteDatabase sdb = dbHelper.getReadableDatabase();
//        String sql = "select * from user where name = ? and password = ?";
//        Cursor cursor = sdb.rawQuery(sql,new String[]{name,password});
//        if(cursor.moveToFirst() == true){
//            cursor.close();
//            return true;
//        }
//        return false;
//    }

    public boolean register(String name,String password,Integer status,Integer type){
        boolean sta = false;
        List<User> userList = getUser(name);//第一次用用户名去查，没有就去注册。
        if(userList.size() == 0){
            boolean bl = this.save(name,password,status,type);
            if(bl){//注册成功
                sta = true;
                Log.v("注册","注册成功");
            }else {//注册失败
                sta = false;
                Log.v("注册","注册失败");
            }
        }else{
            sta = true;
            Log.v("注册","已经注册过了");
        }

        return sta;
    }


//    public boolean registerOrLogin(String name,String password,Integer status,Context myContext){
//        List<User> userList = getUser(name,password);//第一次用用户名去查，没有就去注册。
//        if(userList.size() == 0){
//            boolean bl = this.save(name,password,status);
//            if(bl){//注册成功 去登录
//                login(name,password,myContext);
//            }
//        }else{// 登录
//            login(name,password,myContext);
//        }
//
//        return true;
//    }

    public boolean login(String name,String password,Integer status,Context myContext){
        User loginUser = this.getLoginUser();
        if(loginUser.getId() != null){
            User user = new User(loginUser.getName(),loginUser.getPassword());
            User user0 = new User(0);
            this.updatatype(user0,user);
        }
        List<User> userList = getUser(name,password);
        if(userList.size()>0){
            User user = new User(name,password);
            User user1 = new User(1);
            this.updatatype(user1,user);
            Toast.makeText(myContext,"登录成功！",Toast.LENGTH_LONG).show();
            return true;
        }else {
            Toast.makeText(myContext,"登录失败:密码错误！",Toast.LENGTH_LONG).show();
        }
        return false;
    }

    /**获取单个数据
     * @param name
     * @return
     */
    public List<User> getUser(String name) {
        User user = new User(name);
        userDao = BaseDaoFactory.getInstance().createBaseDao(UserDao.class, User.class);
        listUser.addAll(userDao.query(user));
        return listUser;
    }

    /**获取单个数据
     * @param name
     * @param password
     * @return
     */
    public List<User> getUser(String name, String password) {
        User user = new User(name,password);
        userDao = BaseDaoFactory.getInstance().createBaseDao(UserDao.class, User.class);
        listUser.addAll(userDao.query(user));
        return listUser;
    }

    public User getLoginUser() {
        User user = new User(1);
        userDao = BaseDaoFactory.getInstance().createBaseDao(UserDao.class, User.class);
        listUser.addAll(userDao.query(user));
        if(listUser.size()>0){
            return listUser.get(0);
        }
        return new User();
    }

    public boolean save(String name, String password, Integer status, Integer type) {
        User user = new User(name, password,status,type);
        long size = userDao.insert(user);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public void updata(User user) {
        User user1 = new User();
        userDao.update(user,user1);
    }

    public void updatatype(User user1,User user) {
        userDao.update(user1,user);
    }

}
